import 'package:boss/main.dart';
import 'package:boss/models/commodity.dart';
import 'package:boss/models/printer.dart';
import 'package:boss/models/sku.dart';
import 'package:boss/models/store_menu.dart';
import 'package:boss/models/taste.dart';
import 'package:boss/provider/commodity_provider.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:boss/models/material.dart' as material;
import 'package:provider/provider.dart';

class PlusCommodityByFormProvider extends ChangeNotifier {
  Commodity commodity;
  StoreMenu menu;
  StoreMenu oldMenu;
  List<Printer> printers = [];

  String get materialName {
    if (commodity == null) {
      return '';
    }

    var temp = '';
    commodity.commodityMeterialList.forEach((item) {
      temp += '${item.meterialName}';

      if (commodity.commodityMeterialList.last != item) {
        temp += '、';
      }
    });

    return temp;
  }

  String get tasteName {
    if (commodity == null) {
      return '';
    }

    var temp = '';
    commodity.commodityTasteList.forEach((item) {
      temp += '${item.tasteName}';

      if (commodity.commodityTasteList.last != item) {
        temp += '、';
      }
    });

    return temp;
  }

  PlusCommodityByFormProvider({Commodity commodity}) {
    this.commodity = commodity ?? Commodity.fromJson({'commodityChefPrint': 1, 'commodityMinPie': 1, 'onsale': 1});

    if (commodity != null) {
      var commodityProvider = Provider.of<CommodityProvider>(GlobalPageContext.instance.context, listen: false);

      menu = commodityProvider.storeMenus.firstWhere((item) => item.menuId == commodity.menuId, orElse: () => null);

      oldMenu = StoreMenu.fromJson(menu.toJson());
    }

    final commodityProvider = Provider.of<CommodityProvider>(GlobalPageContext.instance.context, listen: false);

    final storeMenu = commodityProvider.storeMenus.length == 0 ? null : commodityProvider.currentMenuCategory;

    if (commodity == null && storeMenu != null) {
      if (storeMenu.isPackages == 1) {
        menu = commodityProvider.storeMenus[0];
      } else {
        menu = storeMenu;
      }

      oldMenu = StoreMenu.fromJson(menu.toJson());
      this.commodity.menuId = storeMenu.menuId;
      this.commodity.menuName = storeMenu.menuName;
      this.commodity.commodityMeterialList = oldMenu.commodityMeterialList;
      this.commodity.printerInfoList = storeMenu.printerInfoList;
    }
  }

  setCommodityTitle(String title) {
    commodity.title = title;
    notifyListeners();
  }

  setCommodityMenu(StoreMenu menu) {
    this.menu = menu;
    commodity.menuName = menu.menuName;
    commodity.menuId = menu.menuId;
    commodity.printerInfoList = menu.printerInfoList;
    commodity.commodityChefPrint = menu.printerInfoList.length > 0 ? 1 : 0;

    if (commodity.commodityMeterialList.length == 0 || commodity.commodityMeterialList[0].menuId != '') {
      commodity.commodityMeterialList = menu.commodityMeterialList;
    }

    notifyListeners();
  }

  setCommodityPrice(String price) {
    try {
      commodity.price = AmountUtil.string2Price(price);
      notifyListeners();
    } catch (e) {
      commodity.price = null;
    }
  }

  void setCommodityOriginPrice(String price) {
    try {
      commodity.originalPrice = AmountUtil.string2Price(price);
      notifyListeners();
    } catch (e) {
      commodity.originalPrice = null;
    }
  }

  setCommodityVipPrice(String price) {
    try {
      commodity.memberPrice = AmountUtil.string2Price(price);
      notifyListeners();
    } catch (e) {
      commodity.memberPrice = null;
    }
  }

  setCommoditySkus(List<Sku> skus) {
    commodity.commoditySkuList = skus;
    notifyListeners();
  }

  setCommodityTastes(List<Taste> tastes) {
    commodity.commodityTasteList = tastes;
    notifyListeners();
  }

  toggleCommodityChefPrinter() {
    commodity.commodityChefPrint = commodity.isChefPrint ? 0 : 1;
    notifyListeners();
  }

  setCommodityImage(String image) {
    commodity.images = image;
    notifyListeners();
  }

  setCommodityUnit(String unit) {
    commodity.unit = unit == 'cancel' ? null : unit;
    notifyListeners();
  }

  setCommodityBarCode(String code) {
    commodity.commodityBarCode = code;
    notifyListeners();
  }

  setCommodityCode(String code) {
    commodity.commodityCode = code;
    notifyListeners();
  }

  setCommodityMinPie(int number) {
    commodity.commodityMinPie = number;
    notifyListeners();
  }

  setCommodityDesc(String value) {
    commodity.description = value;
    notifyListeners();
  }

  toggleCommoditySale() {
    commodity.onsale = commodity.isOnSale ? 0 : 1;
    notifyListeners();
  }

  toggleCommodityPackaged() {
    commodity.packaged = commodity.isPackaged ? 0 : 1;
    notifyListeners();
  }

  setCommodityMaterial(List<material.Material> materials, int meterialType) {
    commodity.commodityMeterialList = materials;
    commodity.meterialType = meterialType;

    notifyListeners();
  }

  setCommodityDevice(List<Printer> printer) {
    commodity.printerInfoList = printer.map((item) => PrinterInfo.fromJson({'deviceId': item.id, 'deviceName': item.printerName})).toList();
    notifyListeners();
  }

  toggleH5() {
    commodity.isH5Sell = commodity.isH5 ? 0 : 1;
    notifyListeners();
  }

  toggleWeighCommodity() {
    commodity.commodityType = commodity.isWeighCommodity ? Commodity.COMMODITY_TYPE_NORMAL : Commodity.COMMODITY_TYPE_WEIGH;
    commodity.unit = commodity.isWeighCommodity ? '斤' : '份';
    notifyListeners();
  }

  initPrinter() async {
    try {
      var response = await api.queryListPrinter();

      if (!response.successful()) {
        toast(response.msg);
        return;
      }

      printers = response.data.where((item) => item.purpose == 1).toList();

      if (commodity.id == null && printers.length > 0) {
        commodity.printerInfoList = [
          PrinterInfo.fromJson({'deviceName': printers[0].printerName, 'deviceId': printers[0].id})
        ];
      } else if (commodity.id == null && printers.length == 0) {
        commodity.commodityChefPrint = 0;
      }

      notifyListeners();
    } catch (e) {
      toast('未知异常');
    }
  }

  void setCommodityCount(String value) {
    try {
      commodity.buyCount = int.parse(value);
    } catch (e) {
      commodity.buyCount = null;
    }
  }
}
